<template>
  <div>
    <el-button type="primary" icon="el-icon-plus" style="margin:10px" @click="showDiagLog">添加</el-button>
    <!-- el-table中的数据展示的是一列一列的进行的 -->
    <!-- 表格 -->
    <el-table border width="100%" :data="list">
      <el-table-column label="序号" width="80px" align="center" type="index" />
      <el-table-column label="品牌名称" width="width" prop="tmName" />
      <el-table-column label="品牌logo" width="width">
        <!-- 插槽中的row就是 data对应数据中的每一项 -->
        <template slot-scope="{row,$index}">
          <img :src="'http://sph.atguigu.cn'+row.logoUrl" alt="" style="width:100px;height:100px">
        </template>
      </el-table-column>
      <el-table-column label="操作" width="width" prop="prop">
        <template slot-scope="{row,$index}">
          <el-button type="warning" icon="el-icon-edit" size="mini" @click="updateTrideMark(row)">修改</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteTrideMake(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <!--
    layout:分页器的布局
    page-count:显示的页码数
    current-page:当前页
    total:分页器展示数据的条数
    page-size:每一页需要展示多少条条数
    page-sizes:可以设置每一页展示多少条数据
    @size-change:每一页个数发生变化次数触发
    @current-change:点击某一页按钮触发
  -->
    <!-- item
       props:对应列内容的字段名
  -->
    <el-pagination style="margin-top:20px;textAlign:center" :current-page="page" :total="total" :page-size="limit" :page-sizes="[3,5,10]" layout="prev, pager, next, jumper ,->,sizes, total" :page-count="7" @current-change="getPageList" @size-change="handleSizeChange" />
    <!-- 对话框 -->
    <!--
      visible.sync:控制对话框显示与隐藏

    -->
    <el-dialog :title="tmForm.id?'修改品牌':'添加品牌'" :visible.sync="dialogFormVisible">
      <el-form ref="ruleForm" width="80%" :model="tmForm" :rules="rules">
        <el-form-item label="品牌名称" label-width="100px" prop="tmName">
          <el-input v-model="tmForm.tmName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="品牌Logol" label-width="100px" prop="logoUrl">
          <!--
            action属性 ：设置图片上传地址
            :on-success检测图片上传成功 上传成功触发一次
            :before-upload"图片上传之前触发一次
          -->
          <el-upload class="avatar-uploader" action="/dev-api/admin/product/fileUpload" list-type="picture" :show-file-list="true" :on-success="handleAvanterSucess" :before-upload="beforeAvatarUpload">
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateTradeMark">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
export default {
  name: 'TradeMark',
  data() {
    return {
      page: 1,
      limit: 3,
      total: 0,
      list: [],
      dialogFormVisible: false,
      // 手机品牌信息
      tmForm: {
        tmName: '',
        logoUrl: ''
      },
      // 表单验证规则
      rules: {
        tmName: [
          { required: true, message: '请输入品牌名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'change' }
        ],
        logoUrl: [
          { required: true, message: '请选择品牌图片' }
        ]
      }
    }
  },
  // --------生命周期函数-----------
  mounted() {
    this.getPageList()
  },
  methods: {
    // 获取品牌列表数据
    async getPageList(pager = 1) {
      this.page = pager
      const { limit, page } = this
      const result = await this.$API.trademark.reqTrandeMark(page, limit)
      console.log(result)
      if (result.code === 200) {
        this.total = result.data.total
        this.list = result.data.records
      }
    },
    handleSizeChange(limit) {
      console.log(limit)
      this.limit = limit
      this.getPageList()
    },
    showDiagLog() {
      this.tmForm = {
        logoUrl: '',
        tmName: ''
      }
      this.dialogFormVisible = true
    },
    // 修改
    updateTrideMark(row) {
      console.log(row)
      // 将已有的品牌信息赋值给tmForm进行展示
      this.dialogFormVisible = true
      this.tmForm = { ... row }
    },
    // --------图片上传------
    handleAvanterSucess(res, file) {
      this.tmForm.logoUrl = res.data
      // file为上传成功后后端返回给前端的数据
      // res 上传成功后返回给前端的数据
    },
    beforeAvatarUpload(file) {
      // file同一批文件夹对象
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 确定上传按钮
    addOrUpdateTradeMark() {
      // 当全部字段验证通过 再书写业务逻辑
      this.$refs.ruleForm.validate(async(success) => {
        if (success) {
          this.dialogFormVisible = false
          const result = await this.$API.trademark.reqAddOrUpdateMark(this.tmForm)
          if (result.code === 200) {
            this.$message({
              type: 'success',
              message: this.tmForm.id ? '修改品牌成功' : '添加品牌成功'
            })
            this.getPageList(this.tmForm.id ? this.page : 1)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 删除品牌
    deleteTrideMake(row) {
      this.$confirm(`你确定删除${row.tmName}？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 当用户点击确认按钮时触发
        const result = await this.$API.trademark.reqDeleteTradeMark(row.id)
        console.log(result)
        if (result.code === 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getPageList(this.list.length > 1 ? this.page : this.page - 1)
        }
      }).catch(() => {
        // 用户点击取消按钮触发
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      console.log(row)
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
